package com.bc004346.travelus.fragment;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bc004346.travelus.AboutActivity;
import com.bc004346.travelus.BookTravelActivity;
import com.bc004346.travelus.CheckinActivity;
import com.bc004346.travelus.CustomMessageActivity;
import com.bc004346.travelus.MyFlightListActivity;
import com.bc004346.travelus.NoticesActivity;
import com.bc004346.travelus.PBTFlightListActivity;
import com.bc004346.travelus.R;
import com.bc004346.travelus.SettingsActivity;
import com.bc004346.travelus.TripMeDestinationsActivity;
import com.bc004346.travelus.TripMeDisclaimerActivity;
import com.bc004346.travelus.utility.Utility;

import static com.bc004346.travelus.utility.Utility.log;

/**
 * Created by Renats on 3/4/14.
 * Navigation Drawer class
 */
public class NavigationDrawerFragment extends Fragment {

    //<editor-fold desc="Members">

    private DrawerLayout mDrawerLayout;
    private ScrollView mScrollView;
    private ActionBarDrawerToggle mDrawerToggle;
    private Activity mActivity;

    //</editor-fold>

    public void init() {
        try {
            mActivity = getActivity();
            if (mActivity != null) {
                mDrawerLayout = (DrawerLayout) mActivity.findViewById(R.id.drawer_layout);
                mScrollView = (ScrollView) mActivity.findViewById(R.id.activity_fragment_drawer);

                TextView myFlights = (TextView) mActivity.findViewById((R.id.drawerMyFlights));
                TextView bookTravel = (TextView) mActivity.findViewById(R.id.drawerBookTravel);
                TextView checkin = (TextView) mActivity.findViewById(R.id.drawerCheckin);
                TextView pbtTracker = (TextView) mActivity.findViewById(R.id.drawerPbtTracker);
                TextView tripMe = (TextView) mActivity.findViewById(R.id.drawerTripMe);
                TextView settings = (TextView) mActivity.findViewById(R.id.drawerSettings);
                TextView about = (TextView) mActivity.findViewById(R.id.drawerAbout);
                TextView notices = (TextView) mActivity.findViewById(R.id.drawerNotices);

                myFlights.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startOrResumeActivity(new Intent(mActivity, MyFlightListActivity.class), mActivity);
                    }
                });
                pbtTracker.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startOrResumeActivity(new Intent(mActivity, PBTFlightListActivity.class), mActivity);
                    }
                });
                bookTravel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (Utility.showCustomMessage)
                            startOrResumeActivity(new Intent(mActivity, CustomMessageActivity.class), mActivity);
                        else
                            startOrResumeActivity(new Intent(mActivity, BookTravelActivity.class), mActivity);
                    }
                });
                checkin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startOrResumeActivity(new Intent(mActivity, CheckinActivity.class), mActivity);
                    }
                });
                notices.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startOrResumeActivity(new Intent(mActivity, NoticesActivity.class), mActivity);
                    }
                });
                tripMe.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (Utility.showTripMeDisclaimer)
                            startOrResumeActivity(new Intent(mActivity, TripMeDisclaimerActivity.class), mActivity);
                        else
                            startOrResumeActivity(new Intent(mActivity, TripMeDestinationsActivity.class), mActivity);
                    }
                });
                settings.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startOrResumeActivity(new Intent(mActivity, SettingsActivity.class), mActivity);
                    }
                });
                about.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startOrResumeActivity(new Intent(mActivity, AboutActivity.class), mActivity);
                    }
                });

                mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

                setupActionBar();
            }
        } catch (Exception e) {
            log(e, mActivity);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
    }


    public void handleOnPrepareOptionsMenu(Menu menu) {
        boolean itemVisible = !mDrawerLayout.isDrawerOpen(mScrollView);

        for (int index = 0; index < menu.size(); index++) {
            MenuItem item = menu.getItem(index);
            item.setEnabled(itemVisible);
        }
    }

    public void handleOnOptionsItemSelected(MenuItem item) {
        mDrawerToggle.onOptionsItemSelected(item);
    }

    public void syncState() {
        mDrawerToggle.syncState();
    }

    //</editor-fold>

    //<editor-fold desc="Private Methods">

    private void startOrResumeActivity(Intent intent, Activity theActivity) {
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        theActivity.startActivity(intent);
        mDrawerLayout.closeDrawer(mScrollView);
    }

    private void setupActionBar() {
        try {
            ActionBar actionBar = mActivity.getActionBar();

            if (actionBar != null)
                actionBar.setDisplayHomeAsUpEnabled(true);

            mDrawerToggle = new ActionBarDrawerToggle(
                    mActivity,
                    mDrawerLayout,
                    R.drawable.ic_drawer,
                    R.string.dummy_content,
                    R.string.dummy_content
            ) {
                @Override
                public void onDrawerClosed(View drawerView) {
                    mActivity.invalidateOptionsMenu();
                    super.onDrawerClosed(drawerView);
                }

                @Override
                public void onDrawerOpened(View drawerView) {
                    mActivity.invalidateOptionsMenu();
                    super.onDrawerOpened(drawerView);
                }
            };
        } catch (Exception e) {
            log(e, mActivity);
        }
    }

    //</editor-fold>
}
